unit udmsys;

{$mode DELPHI}{$H+}
{.$define USEDB_ZEOS}
{.$define USEDB_UNIDAC}
interface

uses
  Classes,
  SysUtils,
  DateUtils,
  StrUtils,
  FileUtil,
  ExtCtrls,
  ComCtrls,
  Menus,
  IniFiles,
  Controls,
  //mORMot 2
  mormot.core.os,
  mormot.core.Text,
  mormot.core.perf,
  mormot.core.variants,
  mormot.core.base,
  mormot.core.collections,
  mormot.DB.sql,
  mormot.DB.sql.sqlite3,
  mormot.DB.sql.OleDB,
  mormot.DB.sql.oracle,
  mormot.DB.sql.postgres,
  mormot.DB.raw.OleDB,
  mormot.DB.raw.sqlite3,

  {$ifdef USEDB_ZEOS}
  mormot.DB.sql.zeos,
  mormot.DB.rad.zeos,
  {$endif}
  {$ifdef USEDB_UNIDAC}
  mormot.DB.rad.unidac,
  {$endif}
  //i18N
  gettext,
  DefaultTranslator,
  LCLTranslator,
  UniqueInstance;

type

  { Tdmsys }

  Tdmsys = class(TDataModule)
    il16: TImageList;
    mi: TPopupMenu;
    ti: TTrayIcon;
    ui: TUniqueInstance;
  private

  public
    procedure loadDb(const aConfig: string);
    procedure loadDbTree;
    procedure addDbList();
  end;

var
  dmsys: Tdmsys;
  gPath: string;
  gIni: TIniFile;
  gConfig: TDocVariantData;
  gProps: TSqlDBConnectionProperties;

implementation

{$R *.lfm}

{ Tdmsys }
uses
  ufrmmain,
  uConst;

procedure Tdmsys.loadDb(const aConfig: string);
begin
  if IsSQLite3File(aConfig) then
    gprops := TSqlDBSQLite3ConnectionProperties.Create(aConfig, '', '', '')
  else if isjetfile(aConfig) then
    gprops := TSqlDBOleDBJetConnectionProperties.Create(aConfig, '', '', '')
  {$ifdef USEDB_ZEOS}
  else if leftstr(aConfig, 4) = 'zeos' then
    gprops := TSqlDBZeosConnectionProperties.Create(aConfig, '', '', '')
  {$endif}
  else if leftstr(aConfig, 1) = '{' then
    gprops := TSqlDBConnectionProperties.CreateFromJson(aConfig);
  if gprops <> nil then
    loadDbTree;
end;

procedure Tdmsys.loadDbTree;
var
  aTables: TRawUtf8DynArray;
  aFields: TSqlDBColumnDefineDynArray;
  m, n: integer;
  node1, node2, node3: TTreeNode;
begin
  with gProps, frmmain.tvdb.items do
  begin
    addDbList;
    Clear;
    node1 := add(nil, strDbTable);
    GetTableNames(aTables);
    for m := 0 to high(aTables) do
    begin
      node2 := addchild(node1, aTables[m]);
      GetFields(aTables[m], aFields);
      for n := 0 to high(aFields) do
      begin
        node3 := addchild(node2, aFields[n].ColumnName);
      end;
    end;
    node1 := add(nil, strDbView);
    GetViewNames(aTables);
    for m := 0 to high(aTables) do
    begin
      node2 := addchild(node1, aTables[m]);
      GetFields(aTables[m], aFields);
      for n := 0 to high(aFields) do
      begin
        node3 := addchild(node2, aFields[n].ColumnName);
      end;
    end;
  end;
end;

procedure Tdmsys.addDbList();
begin
  with frmmain, tvdb, gIni, gProps do
  begin
    if gProps.DbmsEngineName = 'SQLite' then
      WriteString(DbmsEngineName, DatabaseName, DefinitionToJson())
    else if gProps.DbmsEngineName = 'Jet' then
      WriteString(DbmsEngineName, DatabaseName, DefinitionToJson())
    else if DbmsEngineName = 'zeos' then
      WriteString(DbmsEngineName, ServerName, DefinitionToJson());
  end;
  gIni.UpdateFile;
end;

initialization
  with FormatSettings do
  begin
    DecimalSeparator := '.';
    ThousandSeparator := ',';
    CurrencyDecimals := 2;
    DateSeparator := '-';
    ShortDateFormat := 'yyyy-mm-dd';
    LongDateFormat := 'yyyy-mm-dd';
    TimeSeparator := ':';
    TimeAMString := 'AM';
    TimePMString := 'PM';
    ShortTimeFormat := 'hh:nn';
    LongTimeFormat := 'HH:nn:ss';
    CurrencyString := '$';
  end;
  gini := TIniFile.Create('db.ini');

finalization;
  gini.Free;
end.
